package java101.Cheats;

public class LoopingExpressions {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
				
		System.out.println("For loop: ");
		//For loops have three sections. Initial data type; condition to be met to keep looping; action each time thru loop.
		for (int a=0; a<=10; a++) {
			System.out.print(a + " ");
			//So long as a is less than or equal to 10, add 1 to a. a starts at 0.
			//Leaving off the middle expression (a<10) can cause an infinite loop.
		}
		
		System.out.println();
		System.out.println();
		
		System.out.println("Nested for loops: ");
		for (int b=1; b<=3; b++){
			for (int c=1; c<=3; c++){
				System.out.print(String.format("%dx%d=%d; ", b,c,b*c)); //The first %d is filled with the next expression listed. The second does the next.
			}
		}
	
		System.out.println();
		System.out.println();
		
		//Fibbonachi numbers - 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...
		
		System.out.println("Arrays: "); 
		int[] f = new int[10]; //This array called f has 10 slots to be filled with integers.
		for (int g = 0; g < f.length; g++) { //f.length is the number of slots in the array.
			f[g] = 0; //Every position in the array is initialized to 0
			System.out.print(f[0] + " "); //Print the 1st slot in the array. - The array slots start at 0.
		}
	
		System.out.println();
		System.out.println();
		
		System.out.println("While loop (Infinite): ");
		while (5>4) {
			System.out.println("Without the break, this will repeat infinite times...");
			break;
		}

		System.out.println();
		System.out.println();
		
		System.out.println("While loop (Correct): ");
		int h = 0;
		while (5>4) {
			System.out.print(h++ + ", ");
			if (h >= 4) {
				System.out.println("Because h is equal to 4, the program stops.");
				break;
			}
		}		
		
		System.out.println();
		System.out.println();
		
				System.out.println("Combining Loops: ");
				int fib[] = new int [10];
				for (int i = 0; i < 10; i++) {
					fib[i] = (i<2) ? i : fib[i-2] + fib[i-1];
				}
		
				for (int i : fib) {
					System.out.print(i + ", ");
				}
				
				int i = 0;
				while (i<4) {
					System.out.println(i++);
				}
		
		System.out.println();
		System.out.println();
		
		System.out.println("Do loop (Condition goes at the end): ");
		int j = 20; //The condition is evaluated after the loop is performed - thus the condition will always run at least once.
		do {
			System.out.println(j);
			j++;
		} while (j<4); //We get "20" because it runs the condition once before evaluating whether to stop it.

		
		
		
		}
	
		
	
	}